<template>
  <div>
    <!-- form: inline可以把form变为行内显示 -->
    <el-form :inline="true" class="demo-form-inline" :model="attrForm">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          v-model="attrForm.category1Id"
          :disabled="scene!=0"
          @change="c1Handler"
        >
          <!-- label:显示文字  value:收集表单数据 -->
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="(c1, index) in list1"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="attrForm.category2Id"
          :disabled="scene!=0"
          @change="c2Handler"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="(c2, index) in list2"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="attrForm.category3Id" @change="c3Handler"   :disabled="scene!=0">
          <el-option :label="c3.name" :value="c3.id" v-for="(c3,index) in list3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Category",
  props:['scene'],
  data() {
    return {
      //保存服务器返回的一级分类的数据
      list1: [],
      //二级分类的数据
      list2: [],
      //三级分类的数据
      list3:[],
      //收集三级联动的数据对象
      attrForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  created() {
    //获取一级分类的接口函数
    this.getC1List();
  },
  methods: {
    //获取一级分类---->组件上来就调用了
    async getC1List() {
      try {
        let result = await this.$API.attr.reqCategory1();
        //保存数据
        this.list1 = result.data;
      } catch (error) {}
    },
    //一级分类下拉菜单触发change事件的回调【获取二级分类的数据】
    async c1Handler() {
      //获取二级分类的数据
      const { category1Id } = this.attrForm;
      //清空二级、三级分类的数据[id]
      this.attrForm.category2Id = '';
      this.attrForm.category3Id = '';
      this.list2 = [];
      this.list3 = [];
      //儿子给父组件传递数据
      this.$emit('getCategoryId',{categoryId:category1Id,flag:1});
      //发请求获取二级分类的数据
      try {
        let result = await this.$API.attr.reqCategory2(category1Id);
        this.list2 = result.data;
      } catch (error) {}
    },
    //二级分类下拉菜单触发change事件的回调【获取三级分类的数据】
    async c2Handler() {
      //获取二级分类的id
      const { category2Id } = this.attrForm;
      //清空三级分类的数据 + id
      this.attrForm.category3Id = '';
      this.list3 = [];
      this.$emit('getCategoryId',{categoryId:category2Id,flag:2});
      try {
        //发请求，获取三级分类的数据
        let result = await this.$API.attr.reqCategory3(category2Id);
        this.list3 = result.data;
      } catch (error) {

      }
    },
    //三级分类的change事件
    c3Handler(){
      const {category3Id} = this.attrForm;
      this.$emit('getCategoryId',{categoryId:category3Id,flag:3});
    }
  },
};
</script>

<style scoped></style>
